package com.ruoyi.common.entity.wxpay;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.*;

@EqualsAndHashCode(callSuper = true)
@Data
public class WxConfig extends WXPayConfig {

    private String appID;
    private String mchID;
    //secret
    private String key;
    private byte[] certData;

    public void setCertData(Long shopId) throws Exception {
        File targetFile;
        InputStream certStream;
        if (shopId == 0) {
            targetFile = new File(System.getProperty("user.dir") + "/apiclient_cert.p12");
        } else {
            targetFile = new File(Global.getProfile() + "/cert/" + shopId + "/apiclient_cert.p12");
        }
        if (!targetFile.exists())
        {
            throw new BusinessException("支付证书不存在！");
        }
        certStream = new FileInputStream(targetFile);
        try
        {
            this.certData = new byte[(int) targetFile.length()];
            certStream.read(this.certData);
        }
        catch (IOException e)
        {
            throw e;
        }
        finally
        {
            certStream.close();
        }
    }

    @Override
    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }

    @Override
    IWXPayDomain getWXPayDomain() {
        return new IWXPayDomainImpl();
    }

    @Override
    Boolean getPrimaryDomain() {
        return true;
    }

    @Override
    String getDomain() {
        return "api.mch.weixin.qq.com";
    }

}

